<?php

/**
 * This is the model class for table "adminhtml_Admin".
 *
 * The followings are the available columns in table 'adminhtml_Admin':
 * @property integer $admin_id
 * @property integer $role_id
 * @property string $username
 * @property string $password
 * @property string $nickname
 * @property string $remark
 * @property string $create_time
 * @property string $update_time
 * @property integer $status
 *
 * The followings are the available model relations:
 * @property AdminhtmlRole $role
 * @property AdminhtmlLog[] $adminhtmlLogs
 */
class AdminhtmlAdmin extends AdminhtmlCAR
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return AdminhtmlAdmin the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	public function getResourceName()
	{
		return '管理员';
	}
	
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'adminhtml_admin';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('username, password', 'required'),
			array('role_id, status', 'numerical', 'integerOnly'=>true),
			array('username', 'length', 'min'=>3, 'max'=>64),
			array('username', 'unique'),
			array('password', 'length', 'min'=>7, 'max'=>32),
			//array('password', 'compare', 'compareValue'=>'password_cf', 'enableClientValidation'=>true ),
			array('nickname', 'length', 'max'=>50),
			array('email', 'length', 'max'=>100),
			array('email', 'email'),
			array('remark', 'length', 'max'=>255),
			array('create_time, update_time', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('admin_id, role_id, username, password, nickname, email, remark, create_time, update_time, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'role' => array(self::BELONGS_TO, 'AdminhtmlRole', 'role_id'),
			'adminhtmlLogs' => array(self::HAS_MANY, 'AdminhtmlLog', 'admin_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'admin_id' => 'ID',
			'role_id' => '角色',
			'username' => '账户',
			'password_cr' => '当前密码',
			'password' => '密码',
			'password_cf' => '重复密码',
			'nickname' => '昵称',
			'email' => '邮箱',
			'remark' => '备注',
			'create_time' => '创建时间',
			'update_time' => '最后登录',
			'status' => '状态',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('admin_id',$this->admin_id);
		$criteria->compare('role_id',$this->role_id);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('nickname',$this->nickname,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('remark',$this->remark,true);
		$criteria->compare('create_time',$this->create_time,true);
		$criteria->compare('update_time',$this->update_time,true);
		$criteria->compare('status',$this->status);

		return new CActiveDataProvider(get_class($this), array(
			'sort'=>array('defaultOrder'=>'admin_id DESC'),
			'pagination'=>array('pageSize'=>20),
		));
	}
	
	public function loadByAccount($account=NULL )
	{
		if( !isset($account) ) {
			throw new CHttpException(404,'Can not find administrator by account.');
		
		} else {
			return $this->findByAttributes(array('username'=>$account) );
		}
	}
	
	public function loadRoleIdByAccount($account=NULL )
	{
		if ($this->loadByAccount($account) ) {
			return $this->loadByAccount($account)->getAttribute('role_id');
			
		} else {
			return false;
		}
	}
	
}